<template>
  <div class="box">
    <div class="zhuce">
      <div style="font-size: 28px;font-weight: bold;margin: 0 auto;width: 150px;">注册/
        <router-link style="text-decoration: none;color:#000;" to="/login">登录</router-link>
      </div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号名称" prop="user">
          <el-input prefix-icon="el-icon-user-solid" v-model="ruleForm.user" clearable></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input prefix-icon="el-icon-user-solid" v-model="ruleForm.nickName" clearable></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input prefix-icon="el-icon-user-solid" v-model="ruleForm.phone" clearable></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="userEmail">
          <el-input prefix-icon="el-icon-user-solid" v-model="ruleForm.userEmail" clearable></el-input>
        </el-form-item>
        <el-form-item label="账号密码" prop="password">
          <el-input prefix-icon="iconfont icon-mima" type="password" v-model="ruleForm.password" clearable></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input prefix-icon="iconfont icon-mima" type="password" v-model="ruleForm.checkPass" clearable></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 70px">
          <el-button type="primary" @click="submitForm(ruleForm)">注册</el-button>
          <el-button @click="returnForm">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    var checkUser = (rule, value, callback) => {
      const regUser = /^[a-zA-Z0-9_-]{3,16}$/
      if (value.length < 3) {
        callback(new Error('用户名长度3~16'));
      }
      if (regUser.test(value)) {
        return callback()
      } else {
        callback(new Error('用户名只能是大写字母、小写字母、下划线、减号组成'));
      }
      callback(new Error('用户名不能为空'));
    }
    var checkMobile = (rule, value, callback) => {
      const regUser = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/
      if (regUser.test(value)) {
        return callback()
      }else {
        if (value.length !== 11) {
          callback(new Error('手机号码长度应该是11位'))
        }
        callback(new Error("手机号格式不正确"))
      }
    }
    var checkEmail = (rule, value, callback) => {
      const regUser = /^([a-zA-Z0-9]+[-_]?)+@[a-zA-Z0-9]+\.[a-z]+$/
      if (regUser.test(value)) {
        return callback();
      }
      if (value === undefined || value.length === 0) {
        callback(new Error('邮箱不能为空'));
      } else {
        callback(new Error('邮箱验证不合法'));
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        user: '',
        nickName: '',
        phone: '',
        userEmail: '',
        password: '',
        checkPass: ''

      },
      rules: {
        user: [{validator: checkUser, trigger: 'blur'}],
        phone: [{validator: checkMobile, trigger: 'blur'}],
        userEmail: [{validator: checkEmail, trigger: 'blur'}],
        password: [{validator: validatePass, trigger: 'blur'}],
        checkPass: [{validator: validatePass2, trigger: 'blur'}]
      }
    }
  },
  methods: {
    returnForm() {
      // 返回login界面
      this.$router.push('/login')
    },
    submitForm() {
      this.$api.user.register(this.ruleForm).then((resp) => {
            let data = resp.data;
            if (data.success) {
              this.ruleForm = {};
              this.$message({
                message: data.message,
                type: 'success'
              });
              this.$router.push('/login');
            } else {
              this.$message.error(resp.data.message);
            }
          }
      )
    }
  }
}
</script>
<style scoped>
.box {
//height: 100%; //background-color: #2e4e6e; width: 100%; height: 958px; background-image: url("../assets/login/register.jpg"); background-size: cover;
}

.zhuce {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 400px;
  background-color: rgba(255, 255, 255, 0.5);
  border-radius: 10px;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.5);
}

.el-form-item {
  margin-top: 30px;
  width: 400px;
}

</style>

